package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.dto.TenantRegisterDto;
import cn.itsource.hrm.errorcode.ErrorCode;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.service.IEmployeeService;
import cn.itsource.hrm.service.ITenantService;
import cn.itsource.hrm.util.AssertUtils;
import cn.itsource.hrm.util.StrUtils;
import cn.itsource.hrm.util.encrypt.MD5;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lixufeng
 * @since 2021-07-25
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Value("${hrm.tenant.register.expiredays}")
    private Integer expireDays;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 租户入驻流程
     * @param dto
     */
    @Override
    public void entering(TenantRegisterDto dto) {
        // 获取前端传输的参数
        Employee employee = dto.getEmployee();
        Long mealId = dto.getMealId();
        Tenant tenant = dto.getTenant();
        // 参数校验
        checkParams(employee, tenant);
        // 保存数据
        // 保存租户基本信息
        Date date = new Date();
        initTenant(tenant, date);
        // 保存租户超管
        initEmployee(employee, tenant, date);
        // 保存购买的套餐
        initTenantMeal(mealId, tenant, date);
    }

    private void initTenantMeal(Long mealId, Tenant tenant, Date date) {
        Date expireDate = DateUtils.addDays(date, expireDays);//  7天试用期
        baseMapper.insertTenantMeal(mealId, tenant.getId(), expireDate, Tenant.STATE_UN_PAY);
    }

    private void initEmployee(Employee employee, Tenant tenant, Date date) {
        employee.setState(Employee.STATE_NORMAL);
        employee.setInputTime(date);
        employee.setTenantId(tenant.getId());
        // 设置盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd  = MD5.getMD5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        employee.setType(Employee.TYPE_TENANT_ADMIN);
        employeeService.insert(employee);
    }

    private void initTenant(Tenant tenant, Date date) {
        tenant.setState(Tenant.STATE_PENDING);
        tenant.setRegisterTime(date);
        baseMapper.insert(tenant);
    }

    private void checkParams(Employee employee, Tenant tenant) {
        // 机构名称不能为空
        AssertUtils.isNull(tenant.getCompanyName(), ErrorCode.COMPANY_NAME_NOT_BE_NULL.getMessage());
        // 用户名称不能为空
        AssertUtils.isNull(employee.getUsername(), ErrorCode.USER_NAME_NOT_BE_NULL.getMessage());
        // 手机号不能为空
        AssertUtils.isNull(employee.getTel(), ErrorCode.PHONE_NOT_BE_NULL.getMessage());
        // 信用码合法
        AssertUtils.isLicense(tenant.getCompanyNum(), ErrorCode.COMPANY_NUM_ILLEGAL.getMessage());
        // 手机号是否合法
        AssertUtils.isMobile(employee.getTel(), ErrorCode.PHONE_ILLEGAL.getMessage());
        // 邮箱是否合法
        AssertUtils.isEmail(employee.getEmail(), ErrorCode.EMAIL_ILLEGAL.getMessage());
        // 手机号、邮箱、公司名字不能被注册
        AssertUtils.isFalse(employeeService.existByPhone(employee.getTel()),ErrorCode.PHONE_REGISTER.getMessage());
    }
}
